export class LogSanitizer {
  private static readonly SENSITIVE_FIELDS = [
    'password',
    'token',
    'authorization',
    'credit_card',
    'idCard',
    'phoneNumber',
    'bankAccount',
  ];

  private static readonly BODY_SIZE_LIMIT = 1024 * 10; // 10KB

  static sanitizeRequest(data: any): any {
    if (!data) return data;
    
    // 深拷贝以避免修改原始数据
    const sanitized = JSON.parse(JSON.stringify(data));
    
    // 递归处理对象
    if (typeof sanitized === 'object') {
      Object.keys(sanitized).forEach(key => {
        // 检查是否是敏感字段
        if (this.SENSITIVE_FIELDS.some(field => key.toLowerCase().includes(field))) {
          sanitized[key] = '******';
        } else if (typeof sanitized[key] === 'object') {
          sanitized[key] = this.sanitizeRequest(sanitized[key]);
        }
      });
    }

    return sanitized;
  }

  static truncateData(data: any): string {
    if (!data) return '';
    
    const stringified = typeof data === 'string' ? data : JSON.stringify(data);
    if (stringified.length > this.BODY_SIZE_LIMIT) {
      return stringified.substring(0, this.BODY_SIZE_LIMIT) + '... (truncated)';
    }
    return stringified;
  }
}